package com.xinzhitong.www.controller;

import com.xinzhitong.www.exception.apply.ApplyRepetitionException;
import com.xinzhitong.www.exception.train.*;
import com.xinzhitong.www.exception.trainclassgeneral.NoTrainClassGeneralException;
import com.xinzhitong.www.exception.trainclassnameandno.NoTrainClassNameAndNoException;
import com.xinzhitong.www.exception.traincontact.NoTrainContactException;
import com.xinzhitong.www.exception.traingeneral.NoTrainGeneralException;
import com.xinzhitong.www.exception.trainnameandidbyprofessionid.NoTrainNameAndIdByProfessionIdException;
import com.xinzhitong.www.exception.traintable.NoTrainTableException;
import com.xinzhitong.www.exception.traintable.NoTrainTimeException;
import com.xinzhitong.www.exception.userenrolltrainclasscondition.NoUserEnrollTrainClassConditionException;
import com.xinzhitong.www.service.TrainService;
import com.xinzhitong.www.utils.Message;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 培训控制器
 *
 * @author 魏骆淳  钟伟良
 */
@CrossOrigin
@RestController
@RequestMapping("train")
public class TrainController {

    @Resource
    TrainService trainService;

    /**
     * 获取培训概要
     *
     * @param professionId 证书id
     * @param pageNo       页码
     * @param pageSize     页大小
     * @return 培训概要
     */
    @RequestMapping(value = "getTrainOutline", method = RequestMethod.GET)
    public Message trainOutline(
            @RequestParam("professionId") Integer professionId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "1") Integer pageSize
    ) {
        try {
            return Message.createSuc(trainService.getTrainOutline(professionId, pageNo, pageSize));
        } catch (NullTrainOutlineException e) {
            e.printStackTrace();
            return Message.createErr("培训概要为空");
        }
    }

    /**
     * 获取培训信息
     *
     * @param trainId 培训id
     * @return 培训信息
     */
    @RequestMapping(value = "getTrainInformation", method = RequestMethod.GET)
    public Message trainInformation(
            @RequestParam("trainId") Integer trainId
    ) {
        try {
            return Message.createSuc(trainService.getTrainInformation(trainId));
        } catch (NullTrainInformationException e) {
            e.printStackTrace();
            return Message.createErr("培训信息为空");
        }
    }

    /**
     * 培训报名
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训报名是否成功信息
     */
    @RequestMapping(value = "trainClassApply", method = RequestMethod.POST)
    public Message trainClassApply(
            @RequestParam("trainClassTimeId") Integer trainClassTimeId
    ) {
        String msg = "报名成功";
        try {
            trainService.getTrainClassApply(trainClassTimeId);
            return Message.createSuc(msg);
        } catch (ApplyRepetitionException e) {
            e.printStackTrace();
            msg = "重复报名";
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训课次为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取培训课次信息
     *
     * @param trainId      培训id
     * @param trainClassNo 培训课次号
     * @return 培训课次信息
     */
    @RequestMapping(value = "getTrainClassInformation", method = RequestMethod.GET)
    public Message trainClassInformation(
            @RequestParam("trainId") Integer trainId,
            @RequestParam("trainClassNo") Integer trainClassNo
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassInformation(trainId, trainClassNo));
        } catch (NullTrainClassInformationException e) {
            e.printStackTrace();
            return Message.createErr("培训课次信息为空");
        }
    }

    /**
     * 获取培训地点
     *
     * @param trainId      培训id
     * @param trainClassNo 培训课次号
     * @return 培训地点
     */
    @RequestMapping(value = "getTrainClassLocation", method = RequestMethod.GET)
    public Message trainClassLocation(
            @RequestParam("trainId") Integer trainId,
            @RequestParam("trainClassNo") Integer trainClassNo
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassLocation(trainId, trainClassNo));
        } catch (NullTrainClassLocationException e) {
            e.printStackTrace();
            return Message.createErr("培训课次地点为空");
        }
    }

    /**
     * 获取培训课次时间
     *
     * @param trainId      培训id
     * @param trainClassNo 培训课次号
     * @return 培训课次时间
     */
    @RequestMapping(value = "getTrainClassTime", method = RequestMethod.GET)
    public Message trainClassTime(
            @RequestParam("trainId") Integer trainId,
            @RequestParam("trainClassNo") Integer trainClassNo
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassTime(trainId, trainClassNo));
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            return Message.createErr("培训课次时间为空");
        }
    }


    /**
     * 获取培训课次
     *
     * @param trainId 培训id
     * @return 培训课次
     */
    @RequestMapping(value = "getTrainClass", method = RequestMethod.GET)
    public Message trainClass(
            @RequestParam("trainId") Integer trainId
    ) {
        try {
            return Message.createSuc(trainService.getTrainClass(trainId));
        } catch (NullTrainClassException e) {
            e.printStackTrace();
            return Message.createErr("培训课次为空");
        }
    }

    /**
     * 获取培训课次报名信息
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训课次报名信息
     */
    @RequestMapping(value = "trainClassApplyInformation", method = RequestMethod.GET)
    public Message getTrainClassApplyInformation(
            @RequestParam("trainClassTimeId") Integer trainClassTimeId
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassApplyInformation(trainClassTimeId));
        } catch (NullTrainClassApplyInformationException e) {
            e.printStackTrace();
            return Message.createErr("培训课次报名信息为空");
        }
    }

    /**
     * 条件筛选培训课次和时间
     *
     * @param time         培训时间
     * @param city         培训城市
     * @param trainId      培训id
     * @param trainClassNo 培训课次号
     * @return 培训课次信息
     */
    @RequestMapping(value = "trainGeneral", method = RequestMethod.GET)
    @ResponseBody
    public Message commentReturn(
            @RequestParam(value = "time") String time,
            @RequestParam(value = "city") String city,
            @RequestParam("trainId") int trainId,
            @RequestParam("trainClassNo") Integer trainClassNo
    ) {
        try {
            return Message.createSuc(trainService.getTrainGeneral(time, city, trainId, trainClassNo));
        } catch (NoTrainGeneralException e) {
            e.printStackTrace();
            return Message.createErr("暂无课次");
        }
    }

    /**
     * 获取培训机构联系方式
     *
     * @param trainOrganId 培训id
     * @return 培训机构联系方式
     */
    @RequestMapping(value = "getContact", method = RequestMethod.GET)
    public Message getTrainContact(
            @RequestParam("trainOrganId") Integer trainOrganId
    ) {
        try {
            return Message.createSuc(trainService.getContact(trainOrganId));
        } catch (NoTrainContactException e) {
            e.printStackTrace();
            return Message.createErr("暂无联系方式");
        }
    }

    /**
     * 获取培训表
     *
     * @param year  年
     * @param month 月
     * @return 培训课表
     */
    @RequestMapping(value = "getTable", method = RequestMethod.GET)
    public Message getTable(@RequestParam("year") Integer year,
                            @RequestParam("month") Integer month
    ) {
        try {
            return Message.createSuc(trainService.getUserTrain(year, month));
        } catch (NoTrainTableException e) {
            e.printStackTrace();
            return Message.createErr("这个月没有培训");
        } catch (NoTrainTimeException e) {
            e.printStackTrace();
            return Message.createErr("往后没有培训");
        }
    }

    /**
     * 获取培训系统消息
     *
     * @param id 培训课次时间id
     * @return 培训系统消息
     */
    @RequestMapping(value = "trainSystemMessage", method = RequestMethod.GET)
    public Message getTrainSystemMessage(@RequestParam("id") Integer id) {
        try {
            return Message.createSuc(trainService.getTrainSystemMessage(id));
        } catch (NullTrainSystemMessage e) {
            e.printStackTrace();
            return Message.createErr("暂无培训系统消息");
        }
    }

    /**
     * 下架培训课次
     *
     * @param trainClassId 培训课次id
     * @return 下架是否成功
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "updateTrainClass", method = RequestMethod.PUT)
    public Message updatePost(
            @RequestParam(value = "trainClassId") Integer trainClassId
    ) {
        return Message.createSuc(trainService.underTrainClass(trainClassId));
    }

    /**
     * 删除培训课次
     *
     * @param idList 培训课次id列表
     * @return 成功删除信息
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "deleteTrainClass", method = RequestMethod.DELETE)
    public Message deleteTrainClass(
            @RequestBody List<Integer> idList
    ) {
        return Message.createSuc(trainService.deleteTrainClass(idList));
    }

    /**
     * 获取培训课次
     *
     * @param trainId 培训id
     * @return 培训课次信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "getTrainClassNameAndNo", method = RequestMethod.GET)
    public Message getTrainClassNameAndNo(
            @RequestParam(value = "trainId") Integer trainId
    ) {
        try {
            return Message.createSuc(trainService.selectTrainClassNameAndNo(trainId));
        } catch (NoTrainClassNameAndNoException e) {
            e.printStackTrace();
            return Message.createErr("此培训暂无课时信息");
        }
    }

    /**
     * 获取培训信息
     *
     * @param professionId 工种id
     * @return 培训信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "getTrainNameAndIdByProfessionId", method = RequestMethod.GET)
    public Message getTrainNameAndIdByProfessionId(
            @RequestParam(value = "professionId") Integer professionId
    ) {
        try {
            return Message.createSuc(trainService.selectTrainNameAndIdByProfessionId(professionId));
        } catch (NoTrainNameAndIdByProfessionIdException e) {
            e.printStackTrace();
            return Message.createErr("此工种暂无培训");
        }
    }

    /**
     * 获取培训课次概览 可以关键字模糊搜索
     *
     * @param word     关键字
     * @param state    状态
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 培训课次信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassGeneral", method = RequestMethod.GET)
    public Message postGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "state", required = false, defaultValue = "") String state,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassGeneral(state, word, pageNo, pageSize));
        } catch (NoTrainClassGeneralException e) {
            e.printStackTrace();
            return Message.createErr("查询不到信息");
        }
    }


    /**
     * 获取培训课次报名详情
     *
     * @param trainClassId 培训课次id
     * @return 报名详情
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassCondition", method = RequestMethod.GET)
    public Message trainClassCondition(
            @RequestParam(value = "trainClassId") Integer trainClassId
    ) {
        try {
            return Message.createSuc(trainService.getTrainClassEnrollCondition(trainClassId));
        } catch (NoUserEnrollTrainClassConditionException e) {
            e.printStackTrace();
            return Message.createErr("此培训课次暂无人报名");
        }
    }


    /**
     * 打印培训课次报名详情表
     *
     * @param trainClassId 培训课次id
     * @return 报名详情
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "downloadTrainClassCondition", method = RequestMethod.GET)
    public Message downloadTrainClassCondition(
            @RequestParam(value = "trainClassId") Integer trainClassId
    ) {
        try {
            return Message.createSuc(trainService.downloadTrainClassEnrollCondition(trainClassId));
        } catch (NoUserEnrollTrainClassConditionException e) {
            e.printStackTrace();
            return Message.createErr("此培训课次暂无人报名");
        }
    }


    /**
     * 保存新增培训
     *
     * @param map 培训
     *            trainId           培训id
     *            trainClassNo      培训课次号
     *            startTime         开始时间
     *            endTime           结束时间
     *            requirePopulation 要求人数
     *            fee               培训费用
     *            contact           联系方式
     *            startClock        开始时刻
     *            endClock          结束时刻
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "trainSave", method = RequestMethod.POST)
    public Message saveTrain(@RequestBody Map<String, Object> map) {
        trainService.saveTrain(map);
        String msg = "保存成功";
        return Message.createSuc(msg);
    }

    /**
     * 获取保存的培训信息
     *
     * @return 培训信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainSaved", method = RequestMethod.GET)
    public Message getSavedTrain() {
        try {
            return Message.createSuc(trainService.getSavedTrain());
        } catch (NullTrainSavedException e) {
            return Message.createErr("未保存");
        }
    }

    /**
     * 发布新增培训
     *
     * @param trainId           培训id
     * @param trainClassNo      培训课次号
     * @param startTime         开始时间
     * @param endTime           结束时间
     * @param requirePopulation 要求人数
     * @param fee               培训费用
     * @param contact           联系方式
     * @param startClock        开始时刻
     * @param endClock          结束时刻
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "trainRelease", method = RequestMethod.PUT)
    public Message releaseTrain(
            @RequestParam("trainId") Integer trainId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("fee") double fee,
            @RequestParam("trainClassNo") Integer trainClassNo,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("startClock") String startClock,
            @RequestParam("endClock") String endClock
    ) {
        trainService.releaseTrain(
                trainId,
                trainClassNo,
                startTime,
                endTime,
                requirePopulation,
                fee,
                contact,
                startClock,
                endClock
        );
        String msg = "提交成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 获取培训完整信息
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训完整信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainTotalInformation", method = RequestMethod.GET)
    public Message getTrainTotalInformation(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg;
        try {
            return Message.createSuc(trainService.getTrainTotalInformation(trainClassTimeId));
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训不存在";
        }
        return Message.createErr(msg);
    }

    /**
     * 编辑培训
     *
     * @param trainClassTimeId  培训课次时间id
     * @param trainId           培训id
     * @param trainClassNo      培训课次号
     * @param startTime         开始时间
     * @param endTime           结束时间
     * @param requirePopulation 要求人数
     * @param fee               培训费用
     * @param contact           联系方式
     * @param startClock        开始时刻
     * @param endClock          结束时刻
     */
    @RequiresRoles(value = {"ORGAN"})
    @RequestMapping(value = "trainEdit", method = RequestMethod.PUT)
    public Message editTrain(
            @RequestParam("trainClassTimeId") Integer trainClassTimeId,
            @RequestParam("trainId") Integer trainId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("fee") double fee,
            @RequestParam("trainClassNo") Integer trainClassNo,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime,
            @RequestParam("startClock") String startClock,
            @RequestParam("endClock") String endClock
    ) {
        trainService.editTrain(
                trainClassTimeId,
                trainId,
                trainClassNo,
                startTime,
                endTime,
                requirePopulation,
                fee,
                contact,
                startClock,
                endClock
        );
        String msg = "修改成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 获取培训基本信息
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训基本信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassTimeInformation", method = RequestMethod.GET)
    public Message getTrainClassTimeInformation(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg;
        try {
            return Message.createSuc(trainService.getTrainBasicInformation(trainClassTimeId));
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训信息为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取培训重要指标
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训重要指标
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassTimeIndex", method = RequestMethod.GET)
    public Message getTrainClassTimeIndex(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg;
        try {
            return Message.createSuc(trainService.getTrainIndex(trainClassTimeId));
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训重要指标为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取培训基本信息和重要指标
     *
     * @param trainClassTimeId 培训id
     * @return 培训基本信息和重要指标
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassTimeInformationAndIndex", method = RequestMethod.GET)
    public Message getTrainClassTimeInformationAndIndex(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg;
        try {
            Map<String, Object> trainClassTimeInformation = trainService.getTrainBasicInformation(trainClassTimeId);
            Map<String, Object> trainClassTimeIndex = trainService.getTrainIndex(trainClassTimeId);
            trainClassTimeIndex.put("name", trainClassTimeInformation.get("name"));
            trainClassTimeIndex.put("time", trainClassTimeInformation.get("time"));
            trainClassTimeIndex.put("contact", trainClassTimeInformation.get("contact"));
            return Message.createSuc(trainClassTimeIndex);
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训信息为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取新报名人数
     *
     * @return 人数
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassNewEnroll", method = RequestMethod.GET)
    public Message trainClassNewEnroll(
    ) {
        return Message.createSuc(trainService.getTrainClassNewEnroll());
    }

    /**
     * 获取最近编辑培训课次时间
     *
     * @return 最近编辑培训课次时间
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "recentEditorTrainClassTime", method = RequestMethod.GET)
    public Message getRecentEditorTrainClassTime() {
        try {
            return Message.createSuc(trainService.getRecentEditorTrainClassTime());
        } catch (NullRecentEditorTrainClassTime e) {
            return Message.createErr("无最近编辑培训课次时间");
        }
    }

    /**
     * 获取培训基本信息(发布时间)
     *
     * @param trainClassTimeId 培训课次时间id
     * @return 培训基本信息
     */
    @RequiresRoles(value = {"ORGAN", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "trainClassTimeBasicInformation", method = RequestMethod.GET)
    public Message getTrainClassTimeBasicInformation(@RequestParam("trainClassTimeId") Integer trainClassTimeId) {
        String msg;
        try {
            return Message.createSuc(trainService.getTrainBasicInformation2(trainClassTimeId));
        } catch (NullTrainClassTimeException e) {
            e.printStackTrace();
            msg = "培训信息为空";
        }
        return Message.createErr(msg);
    }

}
